using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Xml.Schema;
using OpenComposite.EII.Repository;
using OpenComposite.EII.Forms;

namespace OpenComposite.EII.Designer.SCA
{
	public partial class FormCreateProcess : Form //DockContent
	{
		#region Initialization
		public FormCreateProcess(DesignController dc)
		{
			InitializeComponent();

			_dc = dc;
			_serviceProvider = dc;

			txtInputSchema.Text = "No Input Schema.";
			txtOutputSchema.Text = "No Output Schema.";

			//this.DockableAreas = WeifenLuo.WinFormsUI.DockAreas.Float;
			//this.ShowHint = WeifenLuo.WinFormsUI.DockState.Float;
			//this.TabText = "Create Orchestration";
		}
		public FormCreateProcess(IServiceProvider serviceProvider)
		{
			InitializeComponent();

			_dc = serviceProvider as DesignController;
			_serviceProvider = serviceProvider;

			txtInputSchema.Text = "No Input Schema.";
			txtOutputSchema.Text = "No Output Schema.";

			//this.DockableAreas = WeifenLuo.WinFormsUI.DockAreas.Float;
			//this.ShowHint = WeifenLuo.WinFormsUI.DockState.Float;
			//this.TabText = "Create Orchestration";
		}
		#endregion

		#region Public Members

		#region Properties
		public string ProcessName
		{
			get { return txtName.Text; }
			set { txtName.Text = value; }
		}
		public string ProcessDescription
		{
			get { return txtDesc.Text; }
			set { txtDesc.Text = value; }
		}
		public BusinessObject InputBusinessObject
		{
			get { return _inputBO; }
		}
		public BusinessObject OutputBusinessObject
		{
			get { return _outputBO; }
		}
		public XmlSchema ProcessInputSchema
		{
			get { return ( _inputBO != null ? _inputBO.Schema : null ); }
		}
		public XmlSchema ProcessOutputSchema
		{
			get { return ( _outputBO != null ? _outputBO.Schema : null ); }
		}
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private Repository.BusinessObject _inputBO = null;
		private Repository.BusinessObject _outputBO = null;
		private DesignController _dc = null;
		private IServiceProvider _serviceProvider;
		#endregion

		#region Methods
		private BusinessObject selectBO()
		{
			BusinessObject bo = null;
			FormSelectRepositoryEx selitem = new FormSelectRepositoryEx(_serviceProvider);
			if ( _dc == null ) {
				selitem.RepositoryID = Global.Status.CurrentRepositoryId;
				selitem.DomainID = Global.Status.CurrentDomainId;
			} else {
				selitem.RepositoryID = _dc.RepositoryID;
				selitem.DomainID = _dc.DomainID;
			}
			selitem.AllowedTypes = new ItemType[] { ItemType.BObject };
			selitem.MultiSelect = false;
			if ( selitem.ShowDialog() == DialogResult.OK ) {
				bo = selitem.SelectedItem as BusinessObject;
			}
			return bo;
		}
		#endregion

		#region Event Handler
		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		private void tsbtnOK_Click(object sender, EventArgs e)
		{
			if ( txtName.Text == "" ) {
				MessageBox.Show("Please enter a name for the Orchestration", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				txtName.Focus();
				return;
			}
			if ( _inputBO == null || _outputBO == null ) {
				string txt = "";
				if ( _inputBO == null && _outputBO == null )
					txt = "Input and Output Schemas";
				else if ( _inputBO == null )
					txt = "Input Schema";
				else
					txt = "Output Schema";

				if ( MessageBox.Show(
					 string.Format("Do you really want to create the Orchestration without {0}?", txt),
					 this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No ) {
					btnSelInputSchema.Focus();
					return;
				}
			}
			this.DialogResult = DialogResult.OK;
			this.Close();
		}

		private void btnSelInputSchema_Click(object sender, EventArgs e)
		{
			_inputBO = selectBO();
			if ( _inputBO == null )
				txtInputSchema.Text = "No Input Schema.";
			else
				txtInputSchema.Text = _inputBO.Name;
		}

		private void btnSelOutputSchema_Click(object sender, EventArgs e)
		{
			_outputBO = selectBO();
			if ( _outputBO == null )
				txtOutputSchema.Text = "No Output Schema.";
			else
				txtOutputSchema.Text = _outputBO.Name;
		}

		#endregion

		private void FormCreateProcess_Load(object sender, EventArgs e)
		{

		}

		#endregion
	}
}